﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class Analytics : System.Web.UI.Page
{
    int museumId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                if (ddlOption.SelectedValue.Equals("0"))
                    BindMuseumData();
                else
                    BindSchoolData();

                ddlMuseum.DataSource = DBAccess.GetMuseumByCity("");
                ddlMuseum.DataBind();
                if (Session["MuseumId"] != null)
                {
                    if (Convert.ToInt32(Session["MuseumId"]) > 0)
                    {
                        ddlMuseum.SelectedValue = Session["MuseumId"].ToString();
                        ddlMuseum.Enabled = false;
                    }
                }

                BindGridData();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindMuseumData()
    {
        ddlMuseum.DataTextField = "MuseumName";
        ddlMuseum.DataValueField = "MuseumId";
        ddlMuseum.DataSource = DBAccess.GetMuseumByCity("");
        ddlMuseum.DataBind();
        BindGridData();
    }

    private void BindSchoolData()
    {
        ddlMuseum.DataTextField = "SchoolName";
        ddlMuseum.DataValueField = "SchoolId";
        ddlMuseum.DataSource = DBAccess.GetAllSchools();
        ddlMuseum.DataBind();
        BindGridData();
    }

    protected void ddlOption_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlOption.SelectedValue.Equals("0"))
            BindMuseumData();
        else
            BindSchoolData();
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindGridData()
    {
        DataSet dsAnalytics = new DataSet();
        DataTable dtVouchers = new DataTable();
        DataTable dtGames = new DataTable();
        try
        {
            if (ViewState["IsFromGame"] != null)
            {
                if (ViewState["IsFromGame"].ToString().Equals("1"))
                {
                    dtGames = DBAccess.GetGameByOption(Convert.ToInt32(ddlOption.SelectedValue), Convert.ToInt32(ddlMuseum.SelectedValue));

                    ddlGame.DataSource = dtGames;
                    ddlGame.DataBind();

                    if (ViewState["GameId"] != null)
                    {
                        ddlGame.SelectedValue = ViewState["GameId"].ToString();
                    }
                }
            }
            else
            {
                dtGames = DBAccess.GetGameByOption(Convert.ToInt32(ddlOption.SelectedValue), Convert.ToInt32(ddlMuseum.SelectedValue));

                ddlGame.DataSource = dtGames;
                ddlGame.DataBind();
            }

            dsAnalytics = DBAccess.GetAnalyticsData(Convert.ToInt32(ddlGame.SelectedValue));

            foreach (DataTable dt in dsAnalytics.Tables)
            {
                switch (dt.TableName.ToLower())
                {
                    case "voucher":
                        if (dt.Rows.Count > 0)
                        {
                            //assign DataTable to GridView
                            GridViewAnalytics.DataSource = dt;
                            ViewState["gvData"] = dt;
                            GridViewAnalytics.DataBind();
                        }
                        else
                            ClearControls();
                        break;
                    case "game":
                        if (dt.Rows.Count > 0)
                        {
                            txtPrice.Text = dt.Rows[0]["GamePrice"].ToString();
                            txtNoOfVoucherDownloads.Text = dt.Rows[0]["VoucherDownloadCount"].ToString();
                            txtInAppDownloadsAndroid.Text = dt.Rows[0]["InAppDownloadCountAndroid"].ToString();
                            txtInAppDownloadsIPhone.Text = dt.Rows[0]["InAppDownloadCountIPhone"].ToString();
                            txtRevenueThroughAndroid.Text = dt.Rows[0]["RevenueThroughAndroid"].ToString();
                            txtRevenueThroughIPhone.Text = dt.Rows[0]["RevenueThroughIPhone"].ToString();
                        }
                        else
                            ClearControls();
                        break;
                }
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }

    private void ClearControls()
    {
        txtRevenueThroughIPhone.Text = "0";
        txtRevenueThroughAndroid.Text = "0";
        txtPrice.Text = "0";
        txtInAppDownloadsAndroid.Text = "0";
        txtInAppDownloadsIPhone.Text = "0";
        txtNoOfVoucherDownloads.Text = "0";
    }

    protected void ddlGame_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["IsFromGame"] = "1";
        ViewState["GameId"] = ddlGame.SelectedValue;
        BindGridData();
    }
    protected void ddlMuseum_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridData();
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewAnalytics.PageIndex = e.NewPageIndex;
        if (ViewState["gvData"] != null)
            GridViewAnalytics.DataSource = (DataTable)ViewState["gvData"];
        GridViewAnalytics.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewAnalytics.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridViewAnalytics.DataSource = dataView;
            GridViewAnalytics.DataBind();
        }
    }
}
